package cn.aiweiyi.qingjing.coupon.settlement.executor.impl;

import cn.aiweiyi.qingjing.coupon.comm.constant.CouponCategory;
import cn.aiweiyi.qingjing.coupon.comm.vo.CouponTemplateSDK;
import cn.aiweiyi.qingjing.coupon.comm.vo.SettlementInfo;

import cn.aiweiyi.qingjing.coupon.settlement.executor.AbstractExecutor;
import cn.aiweiyi.qingjing.coupon.settlement.executor.RuleExecutor;
import cn.aiweiyi.qingjing.coupon.settlement.util.BigDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 折扣优惠券结算规则执行器
 */
@Slf4j
@Component
public class ZhekouExecutor extends AbstractExecutor implements RuleExecutor {
    /**
     * 规则类型标记
     *
     * @return
     */
    @Override
    public CouponCategory ruleConfig() {
        return CouponCategory.ZHEKOU;
    }

    /**
     * 优惠券规则的计算
     *
     * @param settlementInfo 包含了选择的优惠券
     * @return 修正过的结算信息
     */
    @Override
    public SettlementInfo computeRule(SettlementInfo settlementInfo) {
        double goodsSum = retain2Decimals(goodsCostSum(settlementInfo.getGoodsInfos()));
        SettlementInfo probability = processGoodsTypeNotSatisfy(settlementInfo, goodsSum);
        if (null != probability) {
            log.debug("折扣优惠券模板与商品信息不匹配!");
            return probability;
        }
        // 折扣优惠券可以直接使用，没有门槛
        CouponTemplateSDK templateVO = settlementInfo.getCouponAndTemplateInfos().get(0).getTemplate();
        // 取出模板规则中的折扣值
        double quota = (double) templateVO.getRule().getDiscount().getQuota();
        // 计算使用优惠券之后的价格
        settlementInfo.setCost(
                retain2Decimals((goodsSum * (quota * 1.0 / 100))) > minCost() ?
                        retain2Decimals((goodsSum * (quota * 1.0 / 100))) : minCost()
        );
        log.debug("使用折扣优惠券{} To {}", goodsSum, settlementInfo.getCost());
        return settlementInfo;
    }
}
